package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdTecAchievementsDao;
import com.ccp.dev.qbdms.dao.WQbdTecBasicmanageDao;
import com.ccp.dev.qbdms.dao.WQbdTecFundsplansDao;
import com.ccp.dev.qbdms.model.WQbdTecAchievements;
import com.ccp.dev.qbdms.model.WQbdTecBasicmanage;
import com.ccp.dev.qbdms.model.WQbdTecFundsplans;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 课题 信息操作服务类
 *
 * @author zhl
 * @date 2020-05-25 15:16:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdTecBasicmanageService extends BaseService<WQbdTecBasicmanage> {

    @Resource
    private WQbdTecBasicmanageDao dao;
    @Resource
    private WQbdTecFundsplansDao wQbdTecFundsplansDao;
    @Resource
    private WQbdTecAchievementsDao wQbdTecAchievementsDao;



    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdTecBasicmanage> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdTecBasicmanage data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    @Override
    public int add(WQbdTecBasicmanage WQbdTecBasicmanage) {
        addSubData(WQbdTecBasicmanage);
        return super.add(WQbdTecBasicmanage);
    }

    @Override
    public int update(WQbdTecBasicmanage WQbdTecBasicmanage) {
        wQbdTecFundsplansDao.delByRefId(WQbdTecBasicmanage.getId());
        wQbdTecAchievementsDao.delByRefId(WQbdTecBasicmanage.getId());
        addSubData(WQbdTecBasicmanage);
        return super.update(WQbdTecBasicmanage);
    }


    private void addSubData(WQbdTecBasicmanage WQbdTecBasicmanage) {
        // 子表1
        if (BeanUtils.isNotEmpty(WQbdTecBasicmanage.getwQbdTecFundsplans())) {
            if (WQbdTecBasicmanage.getwQbdTecFundsplans().size() > 0) {
                List<WQbdTecFundsplans> wQbdTecFundsplans = WQbdTecBasicmanage.getwQbdTecFundsplans();
                for (WQbdTecFundsplans wQbdTecFundsplan : wQbdTecFundsplans) {
                    wQbdTecFundsplan.setId(ProjectUUIDUtils.getUUIDFor32());
                    wQbdTecFundsplan.setRefid(WQbdTecBasicmanage.getId());
                }
                wQbdTecFundsplansDao.addList(WQbdTecBasicmanage.getwQbdTecFundsplans());
            }
        }

        // 子表2
        if(BeanUtils.isNotEmpty(WQbdTecBasicmanage.getwQbdTecAchievements())){
            if(WQbdTecBasicmanage.getwQbdTecAchievements().size() >0){
                List<WQbdTecAchievements> wQbdTecAchievements = WQbdTecBasicmanage.getwQbdTecAchievements();
                for (WQbdTecAchievements wQbdTecAchievement : wQbdTecAchievements) {
                    wQbdTecAchievement.setId(ProjectUUIDUtils.getUUIDFor32());
                    wQbdTecAchievement.setRefid(WQbdTecBasicmanage.getId());
                }
                wQbdTecAchievementsDao.addList(WQbdTecBasicmanage.getwQbdTecAchievements());
            }
        }
    }

    @Override
    public int delByIdArray(String[] ids) {
        wQbdTecFundsplansDao.delByRefIdArray(ids);
        wQbdTecAchievementsDao.delByRefIdArray(ids);
        return super.delByIdArray(ids);
    }

    public List<WQbdTecBasicmanage> reportList(String id,String tableName){
        return dao.reportList(id, tableName);
    }


    public List<WQbdTecBasicmanage> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    public void addImportData(List<WQbdTecBasicmanage> list) {
        List<WQbdTecFundsplans> subData = new ArrayList<>();
        List<WQbdTecAchievements> subData1 = new ArrayList<>();
        ZhiLiangConst.addCommonFields(list);
        //上报状态
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            a.setfMainunit(a.getFilldept());
            a.setfMainunitid(a.getFilldeptid());
//            fItemstartdate
            String date = a.getfItemstartdate();
            if(StringUtil.isNotEmpty(date)){
                String[] split = date.split(" - ");
                if(split.length>1){
                    try{
                        a.setfResearchcycle(getYear(split[0],split[1]));
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            if (BaseUtil.SystemLevel.GROUPLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            } else if (BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToGroup());
            } else if (BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            }

            List<WQbdTecFundsplans> wQbdTecFundsplans = a.getwQbdTecFundsplans();
            if(null != wQbdTecFundsplans && wQbdTecFundsplans.size()>0){
                wQbdTecFundsplans.forEach(b->{
                    b.setRefid(mainId);
                    b.setId(ProjectUUIDUtils.getUUIDFor32());
                });
                subData.addAll(wQbdTecFundsplans);
            }

            List<WQbdTecAchievements> wQbdTecAchievements = a.getwQbdTecAchievements();
            if(null != wQbdTecAchievements && wQbdTecAchievements.size()>0){
                wQbdTecAchievements.forEach(b->{
                    b.setRefid(mainId);
                    b.setId(ProjectUUIDUtils.getUUIDFor32());
                });
                subData1.addAll(wQbdTecAchievements);
            }

        });
        if (!list.isEmpty()) {
            dao.addList(list);
            if(!subData.isEmpty()){
                wQbdTecFundsplansDao.addList(subData);
            }
            if(!subData1.isEmpty()){
                wQbdTecAchievementsDao.addList(subData1);
            }
        }
    }

    public List<WQbdTecBasicmanage> getAll(HashMap hashMap) {
        return dao.getAll(hashMap);
    }

    public String getYear(String str1,String str2){
        int i = Integer.parseInt(str1);
        int i1 = Integer.parseInt(str2);
        return String.valueOf(i1 - i);
    }



    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdTecBasicmanage getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdTecBasicmanage 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdTecBasicmanage> wQbdTecBasicmanage,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdTecBasicmanage,extendSysFileList);
        List<WQbdTecBasicmanage> all = this.getAll(new HashMap());
        ArrayList<String> list = new ArrayList<>();
        for (WQbdTecBasicmanage qbdTecBasicmanage : all) {
            list.add(qbdTecBasicmanage.getId());
        }
        ArrayList<WQbdTecBasicmanage> wQbdTecBasicmanageArrayList = new ArrayList<>();
        int i = 0;
        for (WQbdTecBasicmanage qbdTecBasicmanage : wQbdTecBasicmanage) {
            if(list.contains(qbdTecBasicmanage.getId())){
                // 如果存在就更新
                i += this.update(qbdTecBasicmanage);
            }else{
                // 如果不存在就 重新插入数据
                qbdTecBasicmanage.setBelongdeptname(StringUtil.isNotEmpty(qbdTecBasicmanage.getFilldeptHospitalId()) ? BaseUtil.getCurrentTenOrgCodePath(qbdTecBasicmanage.getFilldeptHospitalId()) : BaseUtil.getCurrentTenOrgCodePath(qbdTecBasicmanage.getFilldeptid()));
                if(qbdTecBasicmanage.getwQbdTecFundsplans().size()>0){
                    wQbdTecFundsplansDao.addList(qbdTecBasicmanage.getwQbdTecFundsplans());
                }
                if(qbdTecBasicmanage.getwQbdTecAchievements().size()>0){
                    wQbdTecAchievementsDao.addList(qbdTecBasicmanage.getwQbdTecAchievements());
                }
                wQbdTecBasicmanageArrayList.add(qbdTecBasicmanage);
            }
        }
        if(wQbdTecBasicmanageArrayList.size()>0){
            // 新增数据多少
            return super.addList(wQbdTecBasicmanageArrayList);
        } else {
            // 更新成功了几条
            return i;
        }

    }
}
